﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NBusiness.Data.Rules
{
    [Serializable]
    public abstract class ValidationRule : IValidationRule
    {
        private ValidationRuleHandler _rule;
        private ValidationEventArgs _arg;
        private Type _type;
        private string _field;
        private string _errorMessage;
        private ValidationRuleType _ruleType = ValidationRuleType.Error;

        /// <summary>
        /// The type of entity this rule applies to.
        /// </summary>
        public Type Type
        {
            get { return _type; }
            set { _type = value; }
        }
        
        /// <summary>
        /// The field this rule applies to.
        /// </summary>
        public string Field
        {
            get { return _field; }
        }
        
        /// <summary>
        /// The friendly text for this particular validation rule when it is broken.
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
        }

        public virtual ValidationRuleType RuleType 
        {
            get { return _ruleType; }
        }

        protected ValidationRule(Type type, string field, ValidationRuleHandler rule, ValidationEventArgs arg, string errorMessage)
        {
            _type = type;
            _field = field;
            _rule = rule;
            _arg = arg;
            _errorMessage = errorMessage;
        }

        public bool Validate(object value)
        {
            return _rule(value, _arg);
        }
    }
}
